package com.meihua.baoleyi.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.RegBaseActivity;
import com.meihua.baoleyi.utils.MD5;
import com.meihua.baoleyi.utils.ToastUtil;
import com.meihua.baoleyi.utils.ToastUtils;
import com.meihua.baoleyi.utils.UrlContants;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Author: liming
 * Time: 2017/10/17
 */

public class PayPwdActiity extends RegBaseActivity {

    @Bind(R.id.show_zhanghao)
    TextView tvShowZhaohao;
    @Bind(R.id.show_new_pwd)
    EditText etxNewPwd;
    @Bind(R.id.show_new_confirm_pwd)
    EditText etxNewCnfrmPwd;
    @Bind(R.id.text_back)
    ImageView textBack;
    @Bind(R.id.text_title)
    TextView textTitle;
    private String phone;
    Toast toast;

    @Override
    public int getLayoutResource() {
        return R.layout.activity_pay_pwd;
    }

    @Override
    public void onInitView() {
        ButterKnife.bind(this);
        textTitle.setText("重置支付密码");
        phone = myApp.getPhone().toString();
        phone = phone.replace(phone.substring(3, 7), "****");
        tvShowZhaohao.setText(phone);
    }

    @OnClick({R.id.text_back, R.id.paypwd_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_back: //返回
                finish();
                break;
            case R.id.paypwd_submit:
                submit();
                break;
        }
    }

    public void showShortCenter(Context context, String content) {

        if (toast == null) {
            toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            toast.setText(content);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
    }

    private void submit() {
        if (TextUtils.isEmpty(etxNewPwd.getText()) || TextUtils.isEmpty(etxNewPwd.getText().toString().trim())) {
            showShortCenter(this, "密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(etxNewCnfrmPwd.getText()) || TextUtils.isEmpty(etxNewCnfrmPwd.getText().toString().trim())) {
            showShortCenter(this, "确认密码不能为空");
            return;
        }
        if (etxNewPwd.getText().toString().length()>6||etxNewPwd.getText().toString().length()<6){
            showShortCenter(this, "密码必须为6位不连续的数字");
            return;
        }
        if (!etxNewPwd.getText().toString().trim().equals(etxNewCnfrmPwd.getText().toString().trim())){
            showShortCenter(this, "两次输入的密码不一致");
            return;
        }

        /*if(StringUtil.isOrderNumeric()){

        }*/
        // TODO: 2017/7/28  密码不能是连续的数字验证
        //重置支付密码
        String  userid = myApp.getUser_id();
        String  pay_password =etxNewPwd.getText().toString().trim();
        long time=System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳
        String str=String.valueOf(time);
        String token= myApp.getToken().toString();
        String sign_none="id="+userid+"&pay_password="+pay_password+"&timestamp="+str+"&token="+token;
        String sign_yes = MD5.getMessageDigest(sign_none.toString().getBytes()).toUpperCase();

        OkGo.post(UrlContants.HOST+"api/account/setPayPassword")
                .tag(this)
                .params("id",userid) //用户id
                .params("pay_password",pay_password) //支付密码
                .params("timestamp",str)//时间戳
                .params("token",token)//token
                .params("sign",sign_yes)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.optString("code").equals("200")) {
                                showShortCenter(PayPwdActiity.this, json.optString("msg"));
                                ToastUtils.showT(PayPwdActiity.this, "设置成功！");
                                Intent i = new Intent(PayPwdActiity.this, IdentitySafeActivity.class);
                                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(i);
                               // SharedPreUtils.putString(LmsConstant.PAY_PASSWARD, "1", PayPwdActiity.this);
                                finish();
                            } else {
                                showShortCenter(PayPwdActiity.this, "失败，" + json.optString("msg"));
                                ToastUtils.showT(PayPwdActiity.this, "设置失败！");
                                ToastUtil.show(PayPwdActiity.this, json.optString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

}
